<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2021/12/28 16:46
 * Desc:
 */
class Model_Provider_Api_Ticket
{
    public $api = null;
    public $partner_id = null;
    public $log_dir = null;

    public function __construct($partnerId, $providerChannel = '')
    {
        $this->partner_id = $partnerId;
        $this->log_dir = $providerChannel ? $providerChannel.'_log' : 'supplier_provider';
        $app_id = Kohana::$config->load('supplier_provider_ticket.app_id');
        $app_secret = Kohana::$config->load('supplier_provider_ticket.app_secret');
        $api_url = Kohana::$config->load('supplier_provider_ticket.api_url');
        $this->api = new Model_Provider_Api_Stapi($app_id, $app_secret, $api_url);
    }

    public function scenic_spot($params)
    {
        return $this->request($params, 'product/scenic_spot');
    }

    public function product_list($params)
    {
        return $this->request($params, 'product/list');
    }

    public function product_info($params)
    {
        return $this->request($params, 'product/info');
    }

    public function product_calendar($params)
    {
        return $this->request($params, 'product/calendar');
    }

    public function product_change($params)
    {
        return $this->request($params, 'product/change');
    }

    public function order_verify($params)
    {
        return $this->request($params, 'order/verify');
    }

    public function order_create($params)
    {
        return $this->request($params, 'order/create');
    }

    public function order_pay($params)
    {
        return $this->request($params, 'order/pay');
    }

    public function order_query($params)
    {
        return $this->request($params, 'order/query');
    }

    public function order_refund($params)
    {
        return $this->request($params, 'order/refund');
    }

    public function order_cancel($params)
    {
        return $this->request($params, 'order/close');
    }

    public function scenic_tag($params)
    {
        return $this->request($params, 'product/tag');
    }

    public function request($params, $request_uri)
    {
        $params['partnerId'] = $this->partner_id;
        $requestId = microtime(true);
        $logTitle = $request_uri . "({$requestId})";
        Model_Tool_Log::write($this->log_dir.'.ticket_log', json_encode($params), '发起请求：' . $logTitle);
        $rtn = $this->api->http($params, $request_uri);
        if($request_uri == 'product/list'){
            $log_rtn = array(
                'code'=> $rtn['code'],
                'msg'=> $rtn['msg'],
            );
        }else{
            $log_rtn = $rtn;
        }
        Model_Tool_Log::write($this->log_dir.'.ticket_log', json_encode($log_rtn), '请求结果：' . $logTitle);
        return $rtn;
    }

    // 设置景区interface code
    public static function create_scenic_interface_code($scenicId, $comeFrom)
    {
        return json_encode(array('scenicId' => $scenicId, 'comeFrom' => $comeFrom));
    }

    // 设置票务interface code
    public static function create_ticket_interface_code($productId, $comeFrom)
    {
        return json_encode(array('productId' => $productId, 'comeFrom' => $comeFrom));
    }
}